package cn.wikiflyer.beikang.act.cart;

import java.util.ArrayList;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.act.user.bean.BKAddressResultBean;
import cn.wikiflyer.beikang.utils.AddressUtil;

/**地址选择列表
 */
public class UserAddressSelectAct extends BaseActivity{

	private ListView listView1;
	@Override
	public void loadXml() {
		setContentView(R.layout.bk_address_select_lay);
	}

	@Override
	public void init() {
		listView1 = (ListView) findViewById(R.id.listView1);
		listView1.setAdapter(new SelectAdAdapter());
	}

	@Override
	public void setData() {
		
	}
	
	/**地址选择适配器
	 */
	class SelectAdAdapter extends BaseAdapter{
		private ArrayList<BKAddressResultBean> listData = null;
		public SelectAdAdapter() {
			super();
			listData = AddressData.getInstance(ctx).getListData();
		}

		@Override
		public int getCount() {
			if (listData!= null) {
				return listData.size();
			}
			return 0;
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder mViewHolder = null;
			if (convertView == null) {
				mViewHolder = new ViewHolder();
				convertView = LayoutInflater.from(ctx).inflate(R.layout.bk_select_address_item_lay, null);
				mViewHolder.linear = (LinearLayout) convertView.findViewById(R.id.add_item_linear);
				mViewHolder.tv_name = (TextView) convertView.findViewById(R.id.addlist_item_username);
				mViewHolder.tv_phone = (TextView) convertView.findViewById(R.id.addlist_item_userphone);
				mViewHolder.tv_address = (TextView) convertView.findViewById(R.id.addlist_item_useraddress);
				convertView.setTag(mViewHolder);
			}else{
				mViewHolder = (ViewHolder) convertView.getTag();
			}
			final BKAddressResultBean bean = AddressData.getInstance(ctx).getListData().get(position);
			mViewHolder.tv_name.setText(bean.receiver);
			mViewHolder.tv_phone.setText(bean.phone);
			String str_address = new AddressUtil(ctx).matchAddress(bean.sheng, bean.shi, bean.qu);
			mViewHolder.tv_address.setText(str_address + bean.address);
			mViewHolder.linear.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent i1 = new Intent();
					i1.putExtra("bean", bean);
					activity.setResult(2, i1);
					activity.finish();
				}
			});
			return convertView;
		}
	}
	class ViewHolder{
		LinearLayout linear;
		TextView tv_name;
		TextView tv_phone;
		TextView tv_address;
	}
	

}
